package me.qi.kancha.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.qi.kancha.dto.core.DictionaryDTO;
import me.qi.kancha.service.DictionaryService;
import me.qi.kancha.mapper.DictionaryMapper;
import me.qi.kancha.service.base.support.PageData;
import me.qi.kancha.support.U;
import me.qi.kancha.vo.dictionary.DictionaryInsertVo;
import me.qi.kancha.vo.dictionary.DictionaryQueryVo;
import me.qi.kancha.vo.dictionary.DictionaryUpdateVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
* 字典模块-service实现
*
* @author qi
* @date 2022-10-27 04:09:20
*/
@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, DictionaryDTO>
    implements DictionaryService{

    @Override
    public PageData<DictionaryDTO> listPaging(Object obj) {
        DictionaryQueryVo param = (DictionaryQueryVo) obj;
        LambdaQueryWrapper<DictionaryDTO> wrapper = new LambdaQueryWrapper<>();

        //动态查询条件
        if (!ObjectUtils.isEmpty(param.getType())) {
            wrapper.eq(DictionaryDTO::getType, param.getType());
        }
        if (!ObjectUtils.isEmpty(param.getParent())) {
            wrapper.eq(DictionaryDTO::getParent, param.getParent());
        }
        if (!ObjectUtils.isEmpty(param.getKey())) {
            wrapper.like(DictionaryDTO::getName, param.getKey());
        }
        //默认排序方式为 created_time desc 前段未传 order 参数时生效
        Page page = page(param.pageInstance(OrderItem.asc("created_time")), wrapper);
        return new PageData<>(page);
    }

    @Override
    public DictionaryDTO insert(Object obj) {
        DictionaryInsertVo param = (DictionaryInsertVo) obj;
        DictionaryDTO dto = new DictionaryDTO();
        BeanUtils.copyProperties(param, dto);
        save(dto);
        return dto;
    }

    @Override
    public DictionaryDTO update(Object obj) {
        DictionaryUpdateVo param = (DictionaryUpdateVo) obj;
        DictionaryDTO dto = new DictionaryDTO();
        BeanUtils.copyProperties(param, dto);
        updateById(dto);
        return dto;
    }
}




